package org.spring.springboot.app.mail.mail.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.mail.mail.service.ISysuBindService;
import org.spring.springboot.app.user.sysuser.dao.SysUserDao;
import org.spring.springboot.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

@Service
@Transactional
public class SysuBindServiceImpl implements ISysuBindService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysuBindServiceImpl.class);

    @Autowired
    private SysUserDao sysUserDao;
    // key生成器,redis生成key必须用
    @Autowired
    private RedisUtils redisUtils;

    // 序列化对象redis处理方法
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${mail.transport.protocol}")
    private String protocol;
    @Value("${mail.smtp.host}")
    private String host;
    @Value("${mail.smtp.auth}")
    private String auth;
    @Value("${accout}")
    private String accout;
    @Value("${password}")
    private String password;
    /**
     * 邮箱发送方法
     * accout：发送人账号
     *password：发送人密码
     * mailtheme: 邮件主题
     * html:邮件内容(发送html)
     * Addressee：收件人
     */
    @Override
    public MimeMessage createEmail(final String accout,final String password, String mailtheme, String html, String Addressee) throws Exception {
        Properties config=new Properties();
        //验证邮件账号和秘密
        Session session= Session.getInstance(config, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(accout,password);
            }
        });
        //1创建邮件对
        MimeMessage mailMessage = new MimeMessage(session);
        //2设置发件人
        Address from=new InternetAddress(accout,"GAT","UTF-8");
        mailMessage.setFrom(from);
        //3设置收件人
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(Addressee));
        //4设置超送人
        //mailMessage.setRecipients(RecipientType.CC, new InternetAddress("121255@qq.com"));
        //5设置秘送人
        //mailMessage.setRecipients(RecipientType.BCC, new InternetAddress("121255@qq.com"));
        //6邮件发送主题
        mailMessage.setSubject(mailtheme,"UTF-8");


        mailMessage.setContent(html,"text/html; charset=utf-8");  //text/plain法文本
        //8设置邮件发送时间
        mailMessage.setSentDate(new Date());
        //确认设置完毕
        mailMessage.saveChanges();
        return mailMessage;
    }

    @Override
    public void mainEmail(String mailtheme, String html, String Addressee) throws Exception {

        MimeMessage mimeMessage=this.createEmail(accout,password,mailtheme,html,Addressee);
        Session session=mimeMessage.getSession();
        Properties config=session.getProperties();
        config.put("mail.transport.protocol",protocol);
        config.put("mail.smtp.host",host);
        //config.put("mail.smtp.port","25");
        config.put("mail.smtp.auth",auth);
        //config.put("mail.smtp.ssl.enable","false");
        Transport.send(mimeMessage);
    }



}
